#include "DiffuseSample.h"
#include "Application.h"
#include "utils.h"
#include "Utility.h"
#include <thread>
namespace RayTracer
{
	void DiffuseSample::Init()
	{
		mImageParam = ImageParam(800, 500, "DiffuseMaterialGammaCorrect.png");
		isGenerate = false;
		curIndex = 0;
		upColor = vec3(0.5, 0.7, 1.0);
		downColor = vec3(1.0);
		sphereColor = vec3(1.0);

		sphere1 = std::make_shared<Sphere>(vec3(0, 0, -1), 0.5);
		sphere2 = std::make_shared<Sphere>(vec3(0, -100.5, -1), 100);
		world.add(sphere1);
		world.add(sphere2);
	}

	void DiffuseSample::Render()
	{
		int width = mImageParam.mWidth;
		int height = mImageParam.mHeight;
		uint8_t* pixels = new uint8_t[width * height * 3];
		int index = 0;
		for (int j = height - 1; j >= 0; j--) {
			for (int i = 0; i < width; ++i) {
				vec3 color = vec3(0.0f);
				for (int s = 0; s < samples_per_pixel; s++)
				{
					auto u = (i + Random01()) / (width - 1);
					auto v = (j + Random01()) / (height - 1);
					Ray r = camera.GetRay(u, v);
					color += RayColor(r, world, maxDepth);
				}
				auto scale = 1.0 / samples_per_pixel;
				color *= scale;
				
				GammaCorrect(color);
				pixels[index++] = static_cast<uint8_t>(256 * clamp(color.r, 0.0, 0.999));
				pixels[index++] = static_cast<uint8_t>(256 * clamp(color.g, 0.0, 0.999));
				pixels[index++] = static_cast<uint8_t>(256 * clamp(color.b, 0.0, 0.999));
				curIndex++;
			}
		}
		Utils::SaveImage(("Images/" + mImageParam.mName).c_str(), width, height, 3, pixels);
		isGenerate = true;
	}

	void DiffuseSample::ImGui()
	{
		ImGui::Text(u8"Diffuse Material");
		ImGui::Checkbox(u8"Normal", &isNormal);
		if (isNormal)
		{
			samples_per_pixel = 1;
		}
		else
		{
			ImGui::SliderInt(u8"Spp", &samples_per_pixel, 1, 500);
		}
		ImGui::SliderInt(u8"MaxDepth", &maxDepth, 1, 50);

		ImGui::Separator();
		ImGui::Text("Sky");
		ImGui::ColorEdit3(u8"High", &upColor[0]);
		ImGui::ColorEdit3(u8"Low", &downColor[0]);
		ImGui::Separator();

		ImGui::Text("Sphere");
		if (ImGui::CollapsingHeader("Small Sphere")) 
		{
			ImGui::InputFloat3(u8"Small Sphere Pos", &sphere1->center[0]);
			ImGui::SliderFloat(u8"Small Sphere R", &sphere1->radius, 0.0f, 5.0f);
		}
		if (ImGui::CollapsingHeader("Big Sphere")) 
		{
			ImGui::InputFloat3(u8"Big Sphere Pos", &sphere2->center[0]);
			ImGui::SliderFloat(u8"Big Sphere R", &sphere2->radius, 0.0f, 100.0f);
		}
		ImGui::Separator();
		
		auto w = (float)mImageParam.mWidth;
		auto h = (float)mImageParam.mHeight;
		auto progress = ((float)curIndex / (w * h));
		ImGui::ProgressBar(progress);
		if (ImGui::Button(u8"Generate")) {
			std::thread t = std::thread([=]() { Render(); });
			t.detach();
		}
		if (isGenerate) {
			auto mTexHandle = Utils::LoadTexture(("Images/" + mImageParam.mName).c_str());
			Application::GetInstance()->SetTexHandle(mTexHandle);
			isGenerate = false;
			curIndex = 0;
		}

	}

	void DiffuseSample::ShutDown()
	{
		mTexHandle = 0;
		Application::GetInstance()->SetTexHandle(mTexHandle);
	}

	vec3 DiffuseSample::RayColor(const Ray & r, const Hitable& world, int depth)
	{
		HitRecord rec;
		if (depth <= 0) return vec3(0, 0, 0);
		if (world.Hit(r, 0.001, infinity, rec))
		{
			vec3 res;
			if (isNormal)
			{
				res = 0.5f * (rec.normal + vec3(1, 1, 1));

			}
			else
			{
				vec3 target = rec.position + rec.normal + RandomHemiSphere(rec.normal);
				res = 0.5f * RayColor(Ray(rec.position, target - rec.position), world, depth - 1);
			}
			return res;
		}

		vec3 unit_direction = glm::normalize(r.direction());
		auto t = 0.5f*(unit_direction.y + 1.0f);
		return (1.0f - t)*downColor + t * upColor;
	}
}